****************************************************************************
**		Authors: 	Or Tuttnauer & Liran Harsgor
**		Purpose: 	Produce Figure A1 in Kedar, Harsgor & Tuttnauer (JOP)  
**		input:		KHT_countrylevel.dta
*****************************************************************************

log using "figure a1.log", replace

set more off

use "KHT_countrylevel.dta", clear

*** Baseline model (same for all four sections of the figure)
regress enps enpv lnmeddm vlmeddm smd mmm fused upper, robust
	matrix b=e(b)
	matrix V=e(V)
	scalar b1=b[1,1]  		/* The first coefficient (enpv) */
	scalar b3=b[1,3]		/* The interaction's coefficient */
	scalar varb1=V[1,1]		/* The variance of b1 */
	scalar varb3=V[3,3]		/* The variance of b3 */
	scalar covb1b3=V[1,3]	/* The covariance of b1 and b3 */
gen pred_meddm1=b1+b3*lnmeddm

*** % legislators elected in top quartile districts 
*** model and deviation graph (Fig. A1(a)) ***

reg enps enpv lnmeddm tqleg vlmeddm vtqleg smd mmm fused upper, robust
	matrix b=e(b)
	matrix V=e(V)
	scalar b1=b[1,1]  		/* first coefficient (enpv) */
	scalar b4=b[1,4]		/* first interaction's coefficient */
	scalar b5=b[1,5]		/* second interaction's coefficient */
	scalar varb1=V[1,1]		/* variance of b1 */
	scalar varb4=V[4,4]		/* variance of b4 */
	scalar varb5=V[5,5]		/* variance of b5 */
	scalar covb1b4=V[1,4]	/* covariance of b1 and b4 */
	scalar covb1b5=V[1,5]	/* covariance of b1 and b5 */
	scalar covb4b5=V[4,5]	/* covariance of b4 and b5 */	
gen pred_meddm2=b1+b4*lnmeddm+b5*tqleg

gen dev_meddm=pred_meddm1-pred_meddm2

graph twoway scatter dev_meddm tqleg if dpr==1, legend(off) ///
scheme(s1mono) yline(0) ytitle("{&Delta} permissibility", size(small)) ///
xtitle("A. % legislators in top quartile districts")  ///
mlabel(cyear) mlabsize(vsmall)  ///
ylabel(-0.2 -0.1 0 0.1 0.2, labsize(small)) yscale(range(-0.2 0.2))
graph save modelsdev_tqleg.gph, replace

drop pred_meddm2 dev_meddm 

*** SD of district magnitude  
*** model and deviation graph (Fig. A1(b)) ***

reg enps enpv lnmeddm sddm vlmeddm vsddm smd mmm fused upper, robust
	matrix b=e(b)
	matrix V=e(V)
	scalar b1=b[1,1]  		/* first coefficient (enpv) */
	scalar b4=b[1,4]		/* first interaction's coefficient */
	scalar b5=b[1,5]		/* second interaction's coefficient */
	scalar varb1=V[1,1]		/* variance of b1 */
	scalar varb4=V[4,4]		/* variance of b4 */
	scalar varb5=V[5,5]		/* variance of b5 */
	scalar covb1b4=V[1,4]	/* covariance of b1 and b4 */
	scalar covb1b5=V[1,5]	/* covariance of b1 and b5 */
	scalar covb4b5=V[4,5]	/* covariance of b4 and b5 */	
gen pred_meddm2=b1+b4*lnmeddm+b5*sddm

gen dev_meddm=pred_meddm1-pred_meddm2

graph twoway scatter dev_meddm sddm if dpr==1, legend(off) ///
scheme(s1mono) yline(0) ytitle("{&Delta} permissibility", size(small)) ///
xtitle("B. SD(D)") mlabel(cyear) mlabsize(vsmall) ///
ylabel(-0.2 -0.1 0 0.1 0.2, labsize(small)) yscale(range(-0.2 0.2))
graph save modelsdev_sddm.gph, replace

drop pred_meddm2 dev_meddm 

*** % legislators elected in districts greater than 12 seats  
*** model and deviation graph (Fig. A1(c)) ***

reg enps enpv lnmeddm psgt12 vlmeddm vpsgt12 smd mmm fused upper, robust 
	matrix b=e(b)
	matrix V=e(V)
	scalar b1=b[1,1]  		/* The first coefficient (enpv) */
	scalar b4=b[1,4]		/* The first interaction's coefficient */
	scalar b5=b[1,5]		/* The second interaction's coefficient */
	scalar varb1=V[1,1]		/* The variance of b1 */
	scalar varb4=V[4,4]		/* The variance of b4 */
	scalar varb5=V[5,5]		/* The variance of b5 */
	scalar covb1b4=V[1,4]	/* The covariance of b1 and b4 */
	scalar covb1b5=V[1,5]	/* The covariance of b1 and b5 */
	scalar covb4b5=V[4,5]	/* The covariance of b4 and b5 */	
gen pred_meddm2=b1+b4*lnmeddm+b5*psgt12

gen dev_meddm=pred_meddm1-pred_meddm2

graph twoway scatter dev_meddm psgt12 if dpr==1, legend(off) ///
scheme(s1mono) yline(0) ytitle("{&Delta} permissibility", size(small)) ///
xtitle("C. % legislators elected in D>12") ///
mlabel(cyear) mlabsize(vsmall) ///
ylabel(-0.2 -0.1 0 0.1 0.2, labsize(small)) yscale(range(-0.2 0.2))

graph save modelsdev_psgt12.gph, replace

drop pred_meddm2 dev_meddm
 
 
*** skewness of dm distribution  
*** model and deviation graph (Fig. A1(d)) ***

reg enps enpv lnmeddm skewdm vlmeddm vskewdm smd mmm fused upper, robust
	matrix b=e(b)
	matrix V=e(V)
	scalar b1=b[1,1]  		/* The first coefficient (enpv) */
	scalar b4=b[1,4]		/* The first interaction's coefficient */
	scalar b5=b[1,5]		/* The second interaction's coefficient */
	scalar varb1=V[1,1]		/* The variance of b1 */
	scalar varb4=V[4,4]		/* The variance of b4 */
	scalar varb5=V[5,5]		/* The variance of b5 */
	scalar covb1b4=V[1,4]	/* The covariance of b1 and b4 */
	scalar covb1b5=V[1,5]	/* The covariance of b1 and b5 */
	scalar covb4b5=V[4,5]	/* The covariance of b4 and b5 */	
gen pred_meddm2=b1+b4*lnmeddm+b5*skewdm

gen dev_meddm=pred_meddm1-pred_meddm2

graph twoway scatter dev_meddm skewdm if dpr==1, legend(off) ///
scheme(s1mono) yline(0) ytitle("{&Delta} permissibility", size(small)) ///
xtitle("D. Skewness") mlabel(cyear) mlabsize(vsmall)  ///
ylabel(-0.2 -0.1 0 0.1 0.2 0.3 0.4, labsize(small)) yscale(range(-0.2 0.2))

graph save modelsdev_skewdm.gph, replace

drop pred_meddm2 dev_meddm

*Note: for consistency (and simpolicity), all four graphs present DPRS only 
graph combine modelsdev_tqleg.gph modelsdev_sddm.gph modelsdev_psgt12.gph  ///
	modelsdev_skewdm.gph, col(1) commonscheme scheme(s1mono) ysize(20) xsize(12)
graph save "figure a1.gph", replace

log close
